package com.bicon.bdata_service_search.es.repository;

import com.bicon.bdata_service_search.es.entities.QuestionAndAnswer;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;


/**
 * 描述:
 * repository
 *
 * @author zhaoyu
 * @email zhaoyu@vv.cc
 * @create 2018-08-23 10:08
 */
@Component
public interface ElasticQuestionAndAnswerRepository extends ElasticsearchRepository<QuestionAndAnswer, String> {
    //TODO define the search
    Page<QuestionAndAnswer> findAll();

    @Override
    Page<QuestionAndAnswer> findAll(Pageable pageable);
//    List<QuestionAndAnswer> findByAskContent(String title);
//    Page<QuestionAndAnswer> findByask_contentIsLike(String askContent, Pageable pageable);
    Page<QuestionAndAnswer> findByAskTitleIsLike(String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> findByAskContentIsLike(String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> findByAskContentLike(String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> queryByAskContentIsContaining(String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> queryQuestionAndAnswersByAskContentIn(String askTitle, Pageable pageable);
    @Query()
    Page<QuestionAndAnswer> getByAskContent(String askTitle, Pageable pageable);
    @Query("{\"bool\" : {\"must\" : {\"term\" : {\"askContentMd5\" : \"?0\"}}}}")
    Page<QuestionAndAnswer> findByAskContentMd5(String askContentMd5, Pageable pageable);
    @Query("{\"bool\" : {\"must\" : {\"term\" : {\"askContent\" : \"?0\"}}}}")
    Page<QuestionAndAnswer> findByAskContent2(String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> findQuestionAndAnswerByAskContentContains(String askContent, Pageable pageable);
    Page<QuestionAndAnswer> findByAskContentContains(String askContent, Pageable pageable);
//    @Query("{\n" +
//            "  \"bool\" :{\n" +
//            "        \"multi_match\":{\n" +
//            "                \"query\":\"?0\",\n" +
//            "                \"fields\":[\"askContent\"],\n" +
//            "                 \"type\": \"phrase\"\n" +
//            "        }\n" +
//            "  }\n" +
//            "}")
    @Query(name = "search_property_autocomplete",value = "{\n" +
            "  \"multi_match\": {\n" +
            "    \"query\": \"?0\",\n" +
            "\n" +
            "    \"fields\": [\n" +
            "      \"askContent\"\n" +
            "    ]\n" +
            "  }\n" +
            "}")
    Page<QuestionAndAnswer> findByAskContent(String askContent, Pageable pageable);
//    Page<QuestionAndAnswer> searchSimilar(String askTitle, Pageable pageable);
//    @Override
//    Page<QuestionAndAnswer> searchSimilar(QuestionAndAnswer questionAndAnswer, String[] fields, Pageable pageable);

//    Page<QuestionAndAnswer> findByAsk_contentIsLike(String ask_content, Pageable page);
//    Page<QuestionAndAnswer> findByAsk
    //    Page<QuestionAndAnswer> searchSmi
//    @Query( "{\"match_phrase\" : {\"ask_content\" :  \"?0\"}}")
//    Page<QuestionAndAnswer> findByAsk_content(String ask_content, Pageable pageable);
//    Page<QuestionAndAnswer> (String askTitle, Pageable pageable);
    Page<QuestionAndAnswer> search(QueryBuilder query, Pageable pageable);
}
